package br.com.petrobras.iam.plugins.register.factory;

import oracle.iam.platform.OIMClient;
import org.apache.log4j.Logger;

import javax.security.auth.login.LoginException;
import java.util.Hashtable;
import java.util.Properties;


public class PegarClienteOIM {
    private static final Logger LOGGER = Logger.getLogger(PegarClienteOIM.class);

    @SuppressWarnings("deprecation")
    public OIMClient conexao() throws Exception {
        Properties propini = ArquivoPropriedade.carregar("local.properties");

        String HomeDir = propini.getProperty("XL.HomeDir");
        String HomeAuth = propini.getProperty("XL.Auth");
        String HomePlicy = propini.getProperty("XL.Policy");
        String username = propini.getProperty("qwerty");
        String password = propini.getProperty("uiop");
        String ctxFactory = propini.getProperty("ctxFactory");
        String serverURL = propini.getProperty("serverURL");

        System.setProperty("XL.HomeDir", HomeDir);
        System.setProperty("java.security.auth.login.config", HomeAuth);
        System.setProperty("java.security.policy", HomePlicy);

        Hashtable<String, String> env = new Hashtable<String, String>();

        env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, ctxFactory);
        env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, serverURL);

        OIMClient oimClient = new OIMClient(env);

        LOGGER.debug("Logging in");

        try {
            oimClient.login(username, password);

            /// ?????? Ainda nao sei o que fazer aqui !!!!! JR
            // A API eh da Oracle e ela ja apresenta o metodo "deprecated".
            // No entanto, os exemplos da literatura e da documentacao usam ele.

        } catch (LoginException e) {
            e.printStackTrace();
        }
        LOGGER.debug("Log in successful");

        return oimClient;
    }

}